//
//  ScanZhuangXiangBangHuoVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/11/12.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "ScanZhuangXiangBangHuoVC.h"
#import "ScanZhuangXiangCell.h"
#import "DriverAddCell.h"

@interface ScanZhuangXiangBangHuoVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ScanZhuangXiangGoodModel *currentGoodModel;

@end

@implementation ScanZhuangXiangBangHuoVC

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, self.bottomView.nim_width - 60, 44)];
        [nextBtn setBackgroundColor:Color_ThemeColor];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setTitle:@"保存" forState:0];
        [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:nextBtn];
    }
    return _bottomView;
}

- (void)saveClick {
    if (!self.currentGoodModel.g_id.length) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请选择货物"];
        return;
    }
    if (!self.currentGoodModel.take_num.length) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入收容数"];
        return;
    }if (!self.currentGoodModel.num.length) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请输入货物数量"];
        return;
    }
    if (self.model.goods.has_goods) {
        //有货物时，编辑
        if (self.model.rv_num.length) {
            //周转箱
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:self.model.rv_num forKey:@"rv_num"];
            [dic setObject:self.currentGoodModel.take_num forKey:@"take_num"];
            [dic setObject:self.currentGoodModel.num forKey:@"num"];
            [self requestForEditZZ:dic];
        }else {
            //普通包装
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:self.model.pk_id forKey:@"pk_id"];
            [dic setObject:self.model.pk_num forKey:@"pk_num"];
            [dic setObject:self.currentGoodModel.take_num forKey:@"take_num"];
            [dic setObject:self.currentGoodModel.num forKey:@"num"];
            [self requestForEditPT:dic];
        }
    }else {
        //无货物时，新增
        if (self.model.rv_num.length) {
            //周转箱
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:self.model.rv_num forKey:@"rv_num"];
            [dic setObject:self.currentGoodModel.g_id forKey:@"g_id"];
            [dic setObject:self.currentGoodModel.take_num forKey:@"take_num"];
            [dic setObject:self.currentGoodModel.num forKey:@"num"];
            [self requestForAddZZ:dic];
        }else {
            //普通包装
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:self.model.pk_id forKey:@"pk_id"];
            [dic setObject:self.model.pk_num forKey:@"pk_num"];
            [dic setObject:self.currentGoodModel.g_id forKey:@"g_id"];
            [dic setObject:self.currentGoodModel.take_num forKey:@"take_num"];
            [dic setObject:self.currentGoodModel.num forKey:@"num"];
            [self requestForAddPT:dic];
        }
    }
}

//新增周转箱货物
- (void)requestForAddZZ:(NSMutableDictionary *)dic {
    [HttpRequest PostWithApi:ReceiptDeliveryScanZhuangXiangZhouZhuanAdd params:dic progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshScanZhuangXiang object:nil];
            __weak_self;
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"保存成功" delayOperation:^{
                [weakself.rt_navigationController popViewControllerAnimated:YES];
            }];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

//编辑周转箱货物
- (void)requestForEditZZ:(NSMutableDictionary *)dic {
    [HttpRequest PostWithApi:ReceiptDeliveryScanZhuangXiangZhouZhuanEdit params:dic progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshScanZhuangXiang object:nil];
            __weak_self;
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"保存成功" delayOperation:^{
                [weakself.rt_navigationController popViewControllerAnimated:YES];
            }];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

//新增普通箱货物
- (void)requestForAddPT:(NSMutableDictionary *)dic {
    [HttpRequest PostWithApi:ReceiptDeliveryScanZhuangXiangPuTongAdd params:dic progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshScanZhuangXiang object:nil];
            __weak_self;
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"保存成功" delayOperation:^{
                [weakself.rt_navigationController popViewControllerAnimated:YES];
            }];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

//编辑普通箱货物
- (void)requestForEditPT:(NSMutableDictionary *)dic {
    [HttpRequest PostWithApi:ReceiptDeliveryScanZhuangXiangPuTongEdit params:dic progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshScanZhuangXiang object:nil];
            __weak_self;
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"保存成功" delayOperation:^{
                [weakself.rt_navigationController popViewControllerAnimated:YES];
            }];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - self.bottomView.nim_height - NaviHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

-(ScanZhuangXiangGoodModel *)currentGoodModel {
    if (!_currentGoodModel) {
        _currentGoodModel = [ScanZhuangXiangGoodModel new];
        if (self.model.goods.has_goods) {
            _currentGoodModel.g_id = self.model.goods.info.g_id;
            _currentGoodModel.name = self.model.goods.info.name;
            _currentGoodModel.take_num = self.model.goods.info.take_num;
            _currentGoodModel.num = self.model.goods.info.num;
        }
    }
    return _currentGoodModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"绑货";
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;;
    }
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ScanZhuangXiangCell *cell = [ScanZhuangXiangCell ScanZhuangXiangCellWithTableView:tableView];
        cell.model = self.model;
        return cell;
    }else {
        DriverAddCell *cell = [DriverAddCell DriverAddCellWithTableView:tableView];
        if (indexPath.row == 0) {
            cell.titleText = @"选择货物";
            cell.showLine = YES;
            cell.canClick = YES;
            cell.isMust = NO;
            cell.canNOUserOperation = self.model.goods.has_goods;
            cell.contentText = self.currentGoodModel.name ? self.currentGoodModel.name : @"";
            __weak_self;
            cell.clickBlock = ^{
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                [weakself.model.goods.list enumerateObjectsUsingBlock:^(ScanZhuangXiangGoodModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    UIAlertAction *action1 = [UIAlertAction actionWithTitle:obj.name style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        weakself.currentGoodModel.g_id = obj.g_id;
                        weakself.currentGoodModel.name = obj.name;
                        weakself.currentGoodModel.take_num = obj.take_num;
                        [weakself.tableView reloadData];
                    }];
                    [alert addAction:action1];
                }];
                UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                [alert addAction:cancle];
                [weakself presentViewController:alert animated:YES completion:nil];
            };
            return cell;
        }else if (indexPath.row == 1) {
            cell.titleText = @"收容数";
            cell.showLine = YES;
            cell.canClick = NO;
            cell.isMust = NO;
            cell.contentText = self.currentGoodModel.take_num ? self.currentGoodModel.take_num : @"";
            cell.canNOUserOperation = YES;
//            __weak_self;
//            cell.cellBlock = ^(NSString * _Nonnull str) {
//                weakself.currentGoodModel.take_num = str;
//            };
            return cell;
        }else {
            cell.titleText = @"货物数量";
            cell.showLine = NO;
            cell.canClick = NO;
            cell.isMust = NO;
            cell.contentText = self.currentGoodModel.num ? self.currentGoodModel.num : @"";
            __weak_self;
            cell.cellBlock = ^(NSString * _Nonnull str) {
                weakself.currentGoodModel.num = str;
            };
            return cell;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ScanZhuangXiangCell *cell = [ScanZhuangXiangCell ScanZhuangXiangCellWithTableView:tableView];
        cell.model = self.model;
        return cell.height;
    }else {
        DriverAddCell *cell = [DriverAddCell DriverAddCellWithTableView:tableView];
        return cell.height;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
